package com.example.musicbox.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * @author : zhy
 * @since : 2022/8/1
 */
@Configuration
@MapperScan("com.example.musicbox.mapper")
public class MybatisPlusConfig implements MetaObjectHandler {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    /**
     * 插入是自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"pingdata", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"dodata", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"sendDate", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"uploadDate", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"registerdata", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"lastloigndata", LocalDateTime.class,LocalDateTime.now());
    }


    /**
     * 更新时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

    }
}
